<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
    <title>桌面快捷方式</title>
    <link rel="icon" href="">
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
  </head>
  <body>
  	<form class="form-inline">
  		<div class="form-group">
		    <label>名称</label>
		    <input id="name" type="text" class="form-control" />
		</div>
		<div class="form-group">
		    <label>图标</label>
		    <div class="input-group file-input" data-accept="image/*">
		    	<input id="icon" type="text" class="form-control" /><label class="input-group-addon btn btn-primary">浏览...</label>
		    </div>
		</div>
		<div class="form-group">
		    <label>程序</label>
		    <div class="input-group file-input">
		    	<input id="cmd" type="text" class="form-control" /><label class="input-group-addon btn btn-primary">浏览...</label>
		    </div>
		</div>
		<div class="form-group">
		    <label>创建位置</label>
		    <div class="input-group file-input" data-dir="dir">
			   <input id="targetDir" type="text" class="form-control" /><label class="input-group-addon btn btn-primary">浏览...</label>
		    </div>
		</div>
		<div class="form-group">
		    <label></label>
		    <button id="createDesktopLinkBtn" type="button" class="btn btn-primary">创建</button>
		</div>
  	</form>

    <script type="text/javascript" src="../../js/jquery.min.js"></script>
    <script type="text/javascript" src="../../js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../../js/template-web.js"></script>
    <script type="text/javascript" src="../../js/fileinput.js"></script>
    <script type="text/javascript">
	
		$("#createDesktopLinkBtn").click(function(){
			var name=$("#name").val();
			var icon=$("#icon").val();
			var cmd=$("#cmd").val();
			var targetDir=$("#targetDir").val();
			if(!targetDir.endsWith('/')){
				targetDir+='/';
			}
			if(cmd){
				var fs=require("fs");
				var desktopLinkFile=targetDir+name+'.desktop';
				fs.writeFile(desktopLinkFile,`[Desktop Entry]
Name=${name}
GenericName=${name}
Exec=${cmd}
Terminal=false
Icon=${icon}
Type=Application
Version=0.1`,function(err){
					console.error(err);
				});

				//赋予桌面快捷方式执行权限并使用文件管理器打开所在文件夹
				window.top.app.run(`chmod +x "${desktopLinkFile}" && nautilus "${targetDir}"`);

				//关闭程序对话框
				window.top.app.appDialog.modal('hide');
			}
		});

    </script>
  </body>
</html>
